home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / ISSUE08 / DATADICT / PM.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1995-05-07  |  3.7 KB  |  114 lines

  1. unit Pm;
  2.  
  3. interface
  4.  
  5. uses
  6.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  7.   Forms, Dialogs, DdeMan;
  8.  
  9. type
  10.   EProgManError = class(Exception);
  11.  
  12.   TProgMan = class(TComponent)
  13.   private
  14.     FDdeClientConv: TDdeClientConv;
  15.     procedure InitDDEConversation;
  16.     function ExecMacroString(Macro: String): Boolean;
  17.   public
  18.     { Public declarations }
  19.     constructor Create(AOwner: TComponent); override;
  20.     destructor Destroy; override;
  21.     procedure CreateGroup(GroupName: String);
  22.     procedure DeleteGroup(GroupName: String);
  23.     procedure DeleteItem(ItemName: String);
  24.     procedure AddItem(CmdLine, ItemName: String);
  25.   end;
  26.  
  27. implementation
  28.  
  29. uses Utils;
  30.  
  31. const
  32.   { Program Manager DDE macro strings }
  33.   SDDECreateGroup     = '[CreateGroup(%s)]';
  34.   SDDEShowGroup       = '[ShowGroup(%s, 1)]';
  35.   SDDEDeleteGroup     = '[DeleteGroup(%s)]';
  36.   SDDEDeleteItem      = '[DeleteItem(%s)]';
  37.   SDDEAddItem : PChar = '[AddItem(%s, "%s", %s)]';  { likely to be > 255 chars }
  38.  
  39. constructor TProgMan.Create(AOwner: TComponent);
  40. begin
  41.   inherited Create(AOwner);
  42.   InitDDEConversation;          { establish DDE link with ProgMan }
  43. end;
  44.  
  45. destructor TProgMan.Destroy;
  46. begin
  47.   FDdeClientConv.CloseLink;     { terminate DDE link to ProgMan }
  48.   FDdeClientConv.Free;          { Free DDE component }
  49.   inherited Destroy;            { inherited clean up }
  50. end;
  51.  
  52. function TProgMan.ExecMacroString(Macro: String): Boolean;
  53. begin
  54.   PCharAString(Macro);
  55.   Result := FDdeClientConv.ExecuteMacro(@Macro[1], False);
  56. end;
  57.  
  58. procedure TProgMan.InitDDEConversation;
  59. { Establishes a DDE link with Program Manager }
  60. begin
  61.   { create DDE component }
  62.   FDdeClientConv := TDdeClientConv.Create(Self);
  63.   { attempt to establish DDE link with ProgMan }
  64.   if not FDdeClientConv.SetLink('PROGMAN', 'PROGMAN') then
  65.     raise EProgManError.Create('Failed to establish DDE Link');
  66. end;
  67.  
  68. procedure TProgMan.CreateGroup(GroupName: String);
  69. { Creates a Program Manager group with name given by GroupName }
  70. begin
  71.   { attempt to create group }
  72.   if not ExecMacroString(Format(SDDECreateGroup, [GroupName])) then
  73.     raise EProgManError.Create('Could not create group. Group name: ' +
  74.                                  GroupName);
  75.   { attempt to show group }
  76.   if not ExecMacroString(Format(SDDEShowGroup, [GroupName])) then
  77.     raise EProgManError.Create('Could not show group. Group name: ' +
  78.                                 GroupName);
  79. end;
  80.  
  81. procedure TProgMan.DeleteGroup(GroupName: String);
  82. begin
  83.   if not ExecMacroString(Format(SDDEDeleteGroup, [GroupName])) then
  84.     raise EProgManError.Create('Could not delete group. Group name: ' +
  85.                                 GroupName);
  86. end;
  87.  
  88. procedure TProgMan.DeleteItem(ItemName: String);
  89. begin
  90.   if not ExecMacroString(Format(SDDEDeleteItem, [ItemName])) then
  91.     raise EProgManError.Create('Could not delete item. Group name: ' +
  92.                                 ItemName);
  93. end;
  94.  
  95. procedure TProgMan.AddItem(CmdLine, ItemName: String);
  96. { Adds an item to the active Program Manager group.  CmdLine is the path name }
  97. { of the item, and ItemName is the name as it will appear in Program Manager. }
  98. var
  99.   P: PChar;
  100.   PSize: Word;
  101. begin
  102.   { determine amount of memory needed for AddItem DDE string }
  103.   PSize := StrLen(SDDEAddItem) + (Length(CmdLine) * 2) + Length(ItemName) + 1;
  104.   GetMem(P, PSize);               { allocate memory }
  105.   { format AddItem DDE macro string }
  106.   StrFmt(P, SDDEAddItem, [CmdLine, ItemName, CmdLine]);
  107.   { attempt to add item to group }
  108.   if not FDdeClientConv.ExecuteMacro(P, False) then
  109.     raise EProgManError.Create('Could not add item. Item: ' + ItemName);
  110.   FreeMem(P, PSize);              { clean up }
  111. end;
  112.  
  113. end.
  114.